#include <regx52.h>
#include "delay.h"

#define LCD_DATA   P0

sbit LCD_RS = P2^6;
sbit LCD_RW = P2^5;
sbit LCD_EN = P2^7;

void write_byte(unsigned char rs, unsigned char byte) 
{
	LCD_RS = rs;//0 cmd  1 dat
	LCD_RW = 0;
	LCD_EN = 0;
	LCD_DATA = byte;
	LCD_EN = 1;
	delay(40);
	LCD_EN = 0;//执行命令
	delay(40);
}
	
void write_cmd(unsigned char cmd) 
{
	write_byte(0, cmd);
}
void write_data(unsigned char dat) 
{
	write_byte(1, dat);
}

void LCD_Init() 
{
	write_cmd(0x0C);//显示功能开 无光标
	write_cmd(0x38);//8bit 2行 5x7px
	write_cmd(0x01);//清屏
}	

void LCD_Show_Character(unsigned char line, unsigned char pos, unsigned char c) 
{
	//设置显示位置
	write_cmd( (line>1 ? 0xC0 : 0x80) + pos );
	write_data(c);
}

void LCD_Show_String(unsigned char line, unsigned char pos, unsigned char* ptr) 
{
	//设置显示位置
	unsigned char i=0;
	write_cmd( (line>1 ? 0xC0 : 0x80) + pos );
	while(*(ptr+i)) {
		write_data(     *(ptr+i++)   );
	}
}
